package cqrtplm.listener;

import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;

@CustomListener(
        category = "润通",
        description = "设置变量监听"
)
@Slf4j
public class RTCustomSetVariableListener implements ExecutionListener {
    @CustomListenerField(description = "变量名称", defaultValue = "", required = false)
    private FixedValue varName;

    @CustomListenerField(description = "变量值", defaultValue = "", required = false)
    private FixedValue varValue;

    public void notify(DelegateExecution execution) {
        try {
            log.info("开始执行监听 ==> 设置流程变量CustomSetVariableListener");
            if (this.varName != null && this.varValue != null) {
                execution.setVariable(this.varName.getExpressionText(), this.varValue.getExpressionText());
                log.info("正常执行监听结束 <== 设置流程变量CustomSetVariableListener");
            } else {
                log.info("设置流程变量CustomSetVariableListener，监听参数varName或者varValue为空，监听直接返回，不设置流程变量，请给对应参数设置值！");
            }
        } catch (Exception e) {
            log.error("监听CustomSetVariableListener异常：", e);
            throw e;
        }
    }
}
